<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * 4store
 * 
 * Triple store implementation for 4store.
 * Doesn't seem to work as expected due to the following problems:
 * - triple order is not preserved
 * - some SPARQL queries do not seem to return the expected result 
 */
class triplestore_fourstore_FourStore extends triplestore_TripleStore
{
	protected $serverUrl = null;

	// use XML for ask queries, 4store v1.0.3 doesn't return booleans in JSON (fixed in later versions)
	protected $sparqlAskFormat = 'application/sparql-results+xml';
	
	// set soft-limit to -1 to prevent result capping
	protected $sparqlSelectAdditionalParameters = array('soft-limit' => -1);
	protected $sparqlConstructAdditionalParameters = array('soft-limit' => -1);
	protected $sparqlAskAdditionalParameters = array('soft-limit' => -1);
	protected $sparqlDescribeAdditionalParameters = array('soft-limit' => -1);
	
	// parameters: "server_url"
	public function __construct(&$params)
	{
		parent::__construct($params);
		$this->serverUrl = $params['server_url'];
		$this->sparqlEndpointUrl = $this->serverUrl . '/sparql/';
	}
	
	public function replaceGraph($graphUri, $rdf)
	{
		if($rdf instanceof simplerdf_RdfDocument || $rdf instanceof simplerdf_RdfResource)
		{
			$rdf = $rdf->getRdfXml();
		}
		
		$params = array(
			'http' => array(
				'method' => 'PUT',
				'header' => "Content-Type: application/rdf+xml;charset=UTF-8",
				'content' => &$rdf
				)
			);
		
		$ctx = stream_context_create($params);
		@file_get_contents($this->serverUrl . '/data/' . rawurlencode($graphUri), false, $ctx);
	}

	public function deleteGraph($graphUri)
	{
		$params = array(
			'http' => array(
				'method' => 'DELETE',
				'header' => "Content-Type: application/rdf+xml;charset=UTF-8",
				)
			);
		
		$ctx = stream_context_create($params);
		@file_get_contents($this->serverUrl . '/data/' . rawurlencode($graphUri), false, $ctx);
	}
}